package com.example.androidproject.wallpaper.fragment;

import android.content.Intent;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.androidproject.R;
import com.example.androidproject.base.BaseFragment;
import com.example.androidproject.retrofit.RetrofitService;
import com.example.androidproject.utils.RequestConstant;
import com.example.androidproject.utils.RvBottomManager;
import com.example.androidproject.wallpaper.activity.WallpaperContentActivity;
import com.example.androidproject.wallpaper.bean.WallpaperBean;
import com.example.androidproject.wallpaper.adapter.WallpaperAdapter;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class WallpaperFragment extends BaseFragment {
    private static final String TAG = "GGSTUDY";
    private RecyclerView rv_wallpaper;
    private Retrofit retrofit;
    private String classify = "全部";
    private int page = 1;
    private int limit = 15;
    private int total = 0;
    private List<WallpaperBean.DataBean> contentList = new ArrayList<>();
    private WallpaperAdapter wallpaperAdapter;
    private ImageView back_top_btn;
    private RvBottomManager rvBottomManager;

    @Override
    public View initView() {
        Log.e(TAG, "壁纸视图被初始化了");
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_wallpaper, null);
        back_top_btn = view.findViewById(R.id.back_top_btn);
        Glide.with(mContext).load(R.drawable.backtop).into(back_top_btn);
        rv_wallpaper = view.findViewById(R.id.rv_wallpaper);
        retrofit = new Retrofit.Builder().baseUrl(RequestConstant.BASE).addConverterFactory(GsonConverterFactory.create()).build();

        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
        wallpaperAdapter = new WallpaperAdapter(mContext, contentList);
        rv_wallpaper.setLayoutManager(gridLayoutManager);
        rv_wallpaper.setAdapter(wallpaperAdapter);

        wallpaperAdapter.setItemOnClickListener(new WallpaperAdapter.ItemOnClickListener() {
            @Override
            public void onClick(int position) {
                Intent intent = new Intent(mContext, WallpaperContentActivity.class);
                intent.putExtra("classify", contentList.get(position).getClassify());
                intent.putExtra("imgname", contentList.get(position).getTitle());
                startActivity(intent);
            }
        });

        rvBottomManager = new RvBottomManager(mContext, rv_wallpaper, new RvBottomManager.Task() {
            @Override
            public void work() {
                if (total == 0) {
                } else {
                    if (limit * page >= total) {
                        rvBottomManager.setEnd(true);
                        return;
                    }
                }
                page++;
                getContent();
            }
        }, back_top_btn);

        back_top_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rv_wallpaper.smoothScrollToPosition(0);
            }
        });
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        getContent();
    }

    private void getContent() {
        String url = RequestConstant.WALLPAPER_CONTENT_URL + "?classify=" + classify + "&page=" + page + "&limit=" + limit;
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<WallpaperBean> wallpaper = retrofitService.getWallpaper(url);
        wallpaper.enqueue(new Callback<WallpaperBean>() {
            @Override
            public void onResponse(Call<WallpaperBean> call, Response<WallpaperBean> response) {
                if (response.isSuccessful()) {
                    WallpaperBean body = response.body();
                    assert body != null;
                    contentList.addAll(body.getData());
                    wallpaperAdapter.refresh();
                } else {
                    Looper.prepare();
                    Toast.makeText(mContext, "请求失败", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            }

            @Override
            public void onFailure(Call<WallpaperBean> call, Throwable t) {

            }
        });
    }
}
